package com.step.web;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class Netutil {
	//from file
	public static String fromFile(String path) {
        try {
        	File file = new File(MyFileUtil.appPath()+File.separator+"leto.txt");
			System.err.printf("%s,%s\n",file.getAbsolutePath(),file.getCanonicalPath());
        	return FileUtils.readFileToString(file, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
		return null;
	}
	// http://www.lottery.gov.cn/lottery_index_kj.json
	public static String getLast() {
		HttpClient httpClient = HttpClientBuilder.create().build();
		// 创建Get请求
		HttpGet httpGet = new HttpGet("http://www.lottery.gov.cn/lottery_index_kj.json");
		// 响应模型
		HttpResponse response = null;
		try {
			// 由客户端执行(发送)Get请求
			response = httpClient.execute(httpGet);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();
			System.out.println("响应状态为:" + response.getStatusLine());
			if (responseEntity != null) {
				String rel = EntityUtils.toString(responseEntity);
				System.out.println("响应内容长度为:" + responseEntity.getContentLength());
				System.out.println("响应内容为:" + rel);
			    return rel;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放资源
		}
		return null;
	}
}
